<?php

class mapper
{
	private $sql;

	public function __construct()
	{
		$this->sql = new sqlquery;
	}

	public function add_user(entity $entity) 
	{
		$this->sql->set_table("cms_users");
		$this->sql->set_columns("name, password, mail, rank");
		$this->sql->set_values("'".$entity->get_name()."', '".$entity->get_password()."', '".$entity->get_email()."', ".$entity->get_rank());
		$result = $this->sql->insert();

		if($result)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public function edit_user(entity $entity)
	{
		$this->sql->set_table("cms_users");
		$psw = $entity->get_password();
		if(empty($psw)) 
		{
			$this->sql->set_columns("name, mail, rank");
			$array = array(
				$entity->get_name(),
				$entity->get_email(),
				$entity->get_rank()
			);
		}
		else
		{
			$this->sql->set_columns("name, password, mail, rank");
			$array = array(
				$entity->get_name(),
				$entity->get_password(),
				$entity->get_email(),
				$entity->get_rank()
			);
		}

		$this->sql->set_values_array($array);
		$this->sql->set_where("id = ".$entity->get_id());
		$result = $this->sql->update();

		if($result)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public function delete_user($id)
	{
		$this->sql->set_table("cms_users");
		$this->sql->set_where("id = $id");
		$result = $this->sql->delete();

		if($result)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public function block_user($id)
	{

	}

	public function get_user_by_id($id)
	{
		$this->sql->set_table("cms_users");
		$this->sql->set_columns("*");
		$this->sql->set_where("id = $id");
		$result = $this->sql->select();

		if($result)
		{
			return $result;
		}
		else
		{
			return false;
		}
	}

	public function get_all_users()
	{
		$this->sql->set_table("cms_users u, cms_ranks r");
		$this->sql->set_columns("
			u.id AS 'id', u.name AS 'name', u.mail AS 'mail', u.blocked AS 'blocked',
			r.name AS 'rank'
		");
		$this->sql->set_where("u.rank = r.id");
		$this->sql->set_orderby("u.blocked ASC, u.rank ASC, u.id ASC");
		$return = $this->sql->select();

		if($return)
		{
			return $return;
		}
		else
		{
			return false;
		}
	}

	public function add_group(group_entity $group)
	{

	}

	public function edit_group(group_entity $group)
	{

	}

	public function delete_group($id)
	{

	}

	public function get_group_by_id($id)
	{
		
	}

	public function add_rank(rank_entity $rank)
	{

	}

	public function edit_rank(rank_entity $rank)
	{

	}

	public function delete_rank($id)
	{

	}

	public function get_rank_by_id($id)
	{

	}

	public function get_all_ranks()
	{
		$this->sql->set_table("cms_ranks");
		$this->sql->set_columns("*");
		$this->sql->set_orderby("id ASC");
		$result = $this->sql->select();

		if($result)
		{
			return $result;
		}
		else
		{
			return false;
		}
	}
}

?>